package com.oocl.DAOImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.oocl.DAO.IBooksDAO;

import POJO.Books;
import Util.DBUtil;

public class BooksDAOImpl implements IBooksDAO{

	public int addBooks(List<Books> books) {
		String sql = "insert into books(id,name,pub_date,author,price,is_new,publisher) values(?,?,?,?,?,?,?);";
		Connection con = null;
		PreparedStatement pst = null;
		int index = 0;
		try {
			con = DBUtil.getConnection();
			con.setAutoCommit(false);
			pst = con.prepareStatement(sql);
			for (Books book : books) {
				pst.setInt(1, book.getId());
				pst.setString(2, book.getName());
				pst.setDate(3, new java.sql.Date(book.getPubDate().getTime()));
				pst.setString(4, book.getAutor());
				pst.setDouble(5, book.getPrice());
				pst.setBoolean(6, book.isNew());
				pst.setString(7, book.getPublisher());
				pst.addBatch();
			}
			index = pst.executeBatch().length;
			con.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		
		return index;
	}

}
